package main;


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.imageio.ImageIO;

import classes.Point;

public class MainEnterence 
{

	/**
	 * This is like AWFUL programming practices...
	 * I really just need to start working on the actual project because then
	 * the code will be a billion times easier to read and go through...
	 * Plus then I can make sure my class names are spelled correctly...
	 */
	public static void main(String[] args) 
	{
		doesEverything();
	}
	
	public static void doesEverything()
	{
		// TODO Auto-generated method stub

		int BLACK = -16777216;
		int WHITE = -1;
		int GREEN = -16711936;
		int RED = 0xFFFF0000;
		int MOE = 10;
//		int MOEADJ = 10;
		int BADCOUNTLIMIT = 10;
		//int centers [];
//		centers = new int[6];
		//int theCenter = 0;
		ArrayList<Point> theList = new ArrayList<Point>();
		
		
		BufferedImage image = null;
		try
		{
			image = ImageIO.read(new File("img/cam02.jpg"));
		}
		catch (IOException e)
		{
			
		}
		
		/**
		 * Need to figure out how to better calculate where the QR Code is to better do the transformation 
		 */
				
		
		/**
		 * This code is doing the changing of the image to black and white
		 */
		
		//Transform Image to grayscale
		BufferedImage binary = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
		binary.getGraphics().drawImage(image, 0,0, null);
	
		//Find the average color number
		double avg = 0;
		int pixels = 0;
		
		for (int i = 0; i < image.getWidth(); i++)
			for (int j = 0; j < image.getHeight(); j++)
			{
				avg += image.getRGB(i, j);
				pixels++;
			}
		avg = avg/pixels;
		
		
		/**
		 * This code is supposed to narrow down the playing field but it's currently not working at all
		 */
//		
//		int currentWV = 0;
//		int longestWV = 0;
//		int startWV = 0;
//		int currentWH = 0;
//		int longestWH = 0;
//		int startWH = 0;
//		
//		for (int i = 10; i < image.getHeight(); i++)
//		{
//			int j = 10;
//			while(j < image.getWidth())
//			{
//				if(binary.getRGB(j,i) > avg)
//				{
//					currentWV = 1;
//					int count = j + 1;
//					while(count < image.getWidth() && binary.getRGB(count,i) > avg)
//					{
//						currentWV++;
//						count++;
//					}
//					if (currentWV > longestWV)
//					{
//						longestWV = currentWV;
//						startWV = j;
//					}
//					j = count - 1;
//				}
//				j++;
//			}
//		}
//		
//		for (int i = 10; i < image.getWidth(); i++)
//		{
//			int j = 10;
//			while(j < image.getHeight())
//			{
//				if(binary.getRGB(i,j) > avg)
//				{
//					currentWH = 1;
//					int count = j + 1;
//					while(count < image.getHeight() && binary.getRGB(i, count) > avg)
//					{
//						currentWH++;
//						count++;
//					}
//					if (currentWH > longestWH)
//					{
//						longestWH = currentWH;
//						startWH = j;
//					}
//					j = count;
//				}
//				j++;
//			}
//		}
//		
//		image.setRGB(startWV, startWH, RED);
//		image.setRGB(startWV + 1, startWH, RED);
//		image.setRGB(startWV, startWH + 1, RED);
//		image.setRGB(startWV + 1, startWH + 1, RED);
//		
		
		//If the pixel is less than the average it's black, otherwise, it's white
//		for (int i = 0; i < binary.getWidth(); i++)
//			for (int j = 0; j < binary.getHeight(); j++)
//			{
//				if (binary.getRGB(i, j) < avg)
//				{
//					image.setRGB(i, j, BLACK);
//					binary.setRGB(i, j, BLACK);
//				}
//				else
//				{
//					image.setRGB(i, j, WHITE);
//					binary.setRGB(i, j, WHITE);
//				}
//			}
		
		
//			{		if (binary.getRGB(i, j) < THRESHOLD)
//				binary.setRGB(i, j, BLACK);
//			else
//				binary.setRGB(i, j, WHITE);
//	
//			System.out.print(image.getRGB(i,j) + " ");
		//	}
		//if (min > image.getRGB(i,j))
			//		min = image.getRGB(i,j);
		
		
			
		
		//image = new BufferedImage(binary.getWidth(), binary.getHeight(), BufferedImage.TYPE_INT_ARGB);
		//image.getGraphics().drawImage(binary, 0,0, null);
		
		/**
		 * Calculates the average pixel size
		 * Not currently Working
		 */
//		
//		int colorChange = 0;
//		int totalpixelsH = 0;
//		int totalpixelsW = 0;
//		
//		for (int i = 0; i < binary.getWidth(); i++)
//			for (int j = 0; j < binary.getHeight(); j++)
//			{
//				if(binary.getRGB(i, j) == BLACK)
//				{
//					while(j < binary.getHeight() && binary.getRGB(i,j) == BLACK)
//				
//					{
//						j++;
//						totalpixelsH++;
//					}
//		//		else
//			//		while(j < binary.getHeight() && binary.getRGB(i,j) == WHITE)
//				//		j++;
//				colorChange++;
//				}
//			}
//
//		System.out.println(totalpixelsH + " " + colorChange);
//		
//		double averageH = totalpixelsH / colorChange;
//		colorChange = 0;
//		
//		for (int i = 0; i < binary.getHeight(); i++)
//			for (int j = 0; j < binary.getWidth(); j++)
//			{
//				if(binary.getRGB(j, i) == BLACK)
//				{
//					while(j < binary.getWidth() && binary.getRGB(j, i) == BLACK)
//					{
//						j++;
//						totalpixelsW++;
//					}
////				else
////					while(j < binary.getWidth() && binary.getRGB(j, i) == WHITE)
////						j++;
//				colorChange++;
//				}
//			}		
//
//		double averageW = totalpixelsW / colorChange;
//		
//		double totalavg = (averageW + averageH)/8;
//		
//		System.out.println(totalpixelsW + " " + colorChange);
//		
//		System.out.println(averageW + " " + averageH + " = " + totalavg);
//		
//		for (int i = 0; i < totalavg; i++)
//			for (int j = 0; j < totalavg; j++)
//				image.setRGB(i, j, RED);
		
		int heightLoc = 0; 
		int widthLoc = 0;

		/*
		 * Finds those fitting the Finder Pattern Horizontally and then Checks the Vertical
		 * Currently checks just the proportions but once I figure out how to calculate the average
		 * pixel size, I'll be able to better implement this area of the code
		 */
		while(heightLoc < binary.getHeight())
		{
			if (binary.getRGB(widthLoc, heightLoc) == BLACK)
			{
				int black1count = 0;
				int white1count = 0; 
				int black3count = 0;
				int white2count = 0;
				int black2count = 0;
				
				int startWidth = widthLoc;

				while (widthLoc < binary.getWidth() && binary.getRGB(widthLoc, heightLoc) == BLACK)
				{
					black1count++;
					widthLoc++;
				}
				int endBlack = widthLoc;
				
				while(widthLoc < binary.getWidth() && binary.getRGB(widthLoc, heightLoc) == WHITE)
				{
					white1count++;
					widthLoc++;
				}				
				if(widthLoc < binary.getWidth() && black1count > white1count - MOE && black1count < white1count + MOE)
				{
					while(widthLoc < binary.getWidth() && binary.getRGB(widthLoc, heightLoc) == BLACK)
					{
						black3count++;
						widthLoc++;
					}
					if(widthLoc < binary.getWidth() && black3count > (3 * (black1count - MOE)) && black3count < (3 * (black1count + MOE)))
					{
						while(widthLoc < binary.getWidth() && binary.getRGB(widthLoc, heightLoc) == WHITE)
						{
							white2count++;
							widthLoc++;
						}
						if (widthLoc < binary.getWidth() && white2count < black1count + MOE && white2count > black1count - MOE)
						{
							while(widthLoc < binary.getWidth() && binary.getRGB(widthLoc, heightLoc) == BLACK)
							{
								black2count++;
								widthLoc++;
							}
							if (black2count > black1count - MOE && black2count < black1count + MOE)
							{
								int tempTotal = black1count + white1count + black3count + black2count + white2count;
								int tempCenter = widthLoc - tempTotal/2;							
								int tempHeight = heightLoc - tempTotal/2;
								int countBad = 0;
								
								
								if (tempHeight > 0 && tempHeight + tempTotal < binary.getHeight() && tempCenter < binary.getWidth())
								{
									int totalSoFar = tempHeight;
									for (; tempHeight < black1count + totalSoFar; tempHeight++)
										if(binary.getRGB(tempCenter, tempHeight) != BLACK)
											countBad++;
									totalSoFar += black1count;
									for (; tempHeight < white1count + totalSoFar; tempHeight++)
										if(binary.getRGB(tempCenter, tempHeight) != WHITE)
											countBad++;
									totalSoFar += white1count;
									for (; tempHeight < black3count + totalSoFar; tempHeight++)
										if(binary.getRGB(tempCenter, tempHeight) != BLACK)
											countBad++;
									totalSoFar += black3count;
									for (; tempHeight < white2count + totalSoFar; tempHeight++)
										if(binary.getRGB(tempCenter, tempHeight) != WHITE)
											countBad++;
									totalSoFar += white2count;
									for (; tempHeight < black2count + totalSoFar; tempHeight++)
										if(binary.getRGB(tempCenter, tempHeight) != BLACK)
											countBad++;
									
									
									
									
									//System.out.println(countBad);
									if(countBad < BADCOUNTLIMIT && tempTotal > 50)
									{
										Point p = new Point(tempCenter, heightLoc);
//										if(binary.getRGB(p.getX(), p.getY()) == BLACK || binary.getRGB(p.getX(), p.getY() + 1) == BLACK || binary.getRGB(p.getX() + 1, p.getY()) == BLACK)
//											if(binary.getRGB(p.getX(), p.getY() - 1) == BLACK || binary.getRGB(p.getX() - 1, p.getY()) == BLACK)
//											{
												theList.add(p);
												for (int i = startWidth; i < widthLoc; i++)
													image.setRGB(i, heightLoc, GREEN);
//											}
										//image.setRGB(tempCenter, tempHeight, RED);
									}		
								}
							}
						}
					}
				}
				widthLoc = endBlack;
								
			}
			
			widthLoc++;
			
			if (widthLoc >= binary.getWidth())
			{
				widthLoc = 0;
				heightLoc++;
			}
		}
		

		
		for (int i = 0; i < theList.size() - 1; i++)
		{
			Point one = (Point) theList.get(i);
			Point two = (Point) theList.get(i + 1);
			if (one.getX() > two.getX() + 1 && one.getX() < two.getX() - 1)
				if (one.getY() > two.getY() + 1 && one.getY() < two.getY() - 1)
				{
					theList.remove(i);
					i--;
				}
			//System.out.println(theList.get(i).toString());
		}
//		
//
//		for (int i = 0; i < theList.size() - 1; i++)
//		{
//			Point one = (Point) theList.get(i);
//			Point two = (Point) theList.get(i + 1);
//			if (one.getX() <= two.getX() + 1 && one.getX() >= two.getX() - 1)
//				if (one.getY() <= two.getY() + 1 && one.getY() >= two.getY() - 1)
//				{
//					theList.remove(i);
//					i--;
//				}
//			//System.out.println(theList.get(i).toString());
//		}
//
//		for (int i = 0; i < theList.size() - 2; i++)
//		{
//			Point one = (Point) theList.get(i);
//			Point two = (Point) theList.get(i + 2);
//			if (one.getX() <= two.getX() + 1 && one.getX() >= two.getX() - 1)
//				if (one.getY() <= two.getY() + 1 && one.getY() >= two.getY() - 1)
//				{
//					theList.remove(i);
//					i--;
//				}
//			//System.out.println(theList.get(i).toString());
//		}
////
//		int listLength = theList.size();
//		for (int i = 0; i < listLength; i++)
//		{
//			Point temp = (Point) theList.get(0);
//			theList.remove(0);
//			int tempCount = 0;
//			for (int j = 0; j < theList.size(); j++)
//			{
//				Point test = (Point) theList.get(j);
//				if (temp.getX() <= test.getX() + 2 && temp.getX() >= test.getX() - 2 || temp.getY() <= test.getY() + 2 && temp.getY() >= test.getY() - 2)
//					tempCount++;
//			}
//			if(tempCount > 0)
//				theList.add(theList.size(), temp);
//				
//		}
		
		
		for (int i = 0; i < theList.size(); i++)
		{
			Point temp = (Point) theList.get(i);
			image.setRGB(temp.getX(), temp.getY(), RED);
			image.setRGB(temp.getX(), temp.getY() + 1, RED);
			image.setRGB(temp.getX() + 1, temp.getY(), RED);
			image.setRGB(temp.getX() + 1, temp.getY() + 1, RED);
			System.out.println(temp.toString());
		}
		
		// widthLoc = 0;
// 		heightLoc = 0;
// 		
// 		/**
// 		 * Finds those fitting the Finder Pattern Vertically
// 		 */
// 		while(widthLoc < binary.getWidth() && heightLoc < binary.getHeight())
// 		{
// 
// 			if (binary.getRGB(widthLoc, heightLoc) == BLACK)
// 			{
// 				int black1count = 0;
// 				int white1count = 0; 
// 				int black3count = 0;
// 				int white2count = 0;
// 				int black2count = 0;
// 				
// 				int startHeight = heightLoc;
// 
// 				while (heightLoc < binary.getHeight() && binary.getRGB(widthLoc, heightLoc) == BLACK)
// 				{
// 					black1count++;
// 					heightLoc++;
// 				}
// 				int endBlack = heightLoc;
// 				
// 
// 				while(heightLoc < binary.getHeight() && binary.getRGB(widthLoc, heightLoc) == WHITE)
// 				{
// 					white1count++;
// 					heightLoc++;
// 				}
// 				
// 				if(heightLoc < binary.getHeight() && black1count > white1count - MOE && black1count < white1count + MOE)
// 				{
// 					while(heightLoc < binary.getHeight() && binary.getRGB(widthLoc, heightLoc) == BLACK)
// 					{
// 						black3count++;
// 						heightLoc++;
// 					}
// 
// 					if(heightLoc < binary.getHeight() && black3count > (3 * (black1count - MOE)) && black3count < (3 * (black1count + MOE)))
// 					{
// 						while(heightLoc < binary.getHeight() && binary.getRGB(widthLoc, heightLoc) == WHITE)
// 						{
// 							white2count++;
// 							heightLoc++;
// 						}
// 						
// 						if (heightLoc < binary.getHeight() && white2count < black1count + MOE && white2count > black1count - MOE)
// 						{
// 							while(heightLoc < binary.getHeight() && binary.getRGB(widthLoc, heightLoc) == BLACK)
// 							{
// 								black2count++;
// 								heightLoc++;
// 							}
// 							
// 							if (black2count > black1count - MOE && black2count < black1count + MOE)
// 							{
// 								for (int i = startHeight; i < heightLoc; i++)
// 									image.setRGB(widthLoc, i, GREEN);
// 							}
// 						}
// 					}
// 				}
// 				heightLoc = endBlack;
// 								
// 			}
// 			
// 			heightLoc++;
// 			
// 			if (heightLoc >= binary.getHeight())
// 			{
// 				heightLoc = 0;
// 				widthLoc++;
// 			}
// 		}
// 		
// 		widthLoc = 0; 
// 		heightLoc = 0;
// 		
// 		while(heightLoc < image.getHeight() && widthLoc < image.getWidth())
// 		{
// 			
// 			if(image.getRGB(widthLoc, heightLoc) == GREEN)
// 			{
// 				
// 				boolean area = true;
// 				for (int i = widthLoc; i < widthLoc + MOEADJ; i++)
// 					for (int j = heightLoc; j < heightLoc + MOEADJ; j++)
// 						if (i >= image.getWidth() || j >= image.getHeight() || image.getRGB(i, j) != GREEN)
// 							area = false;
// 				if (area)
// 				{
// 					for (int i = widthLoc; i < widthLoc + MOEADJ/*image.getRGB(i,heightLoc) == GREEN*/; i++)
// 						for (int j = heightLoc; j < heightLoc + MOEADJ/*image.getRGB(i,j) == GREEN*/; j++)
// 							image.setRGB(i, j, RED);
// 				}
// 			}
// 			
// 			widthLoc++;
// 			if(widthLoc >= image.getWidth())
// 			{
// 				widthLoc = 0;
// 				heightLoc++;
// 			}
// 		}
// 		
		
		
		
		/**
		 * Temporary Display Components
		 */
		JFrame display = new JFrame();
		
		JScrollPane scroller;
		display.setVisible(true);
		display.setSize(image.getWidth() * 2, image.getHeight() * 2 + 50);
		JPanel picture = new JPanel();
		JLabel labelI = new JLabel(new ImageIcon(image));
		JLabel labelB = new JLabel(new ImageIcon(binary));
		picture.add(labelI);
		picture.add(labelB);
		scroller = new JScrollPane(picture);
		display.add(scroller);
	}
	

}
